package com.idopartx.phonelightning

import android.content.Intent
import android.content.IntentFilter
import android.database.ContentObserver
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.dotools.privacy.PraiseDialog
import com.dotools.privacy.WebFeedBackActivity
import com.idopartx.phonelightning.databinding.ActivityMainBinding
import com.idopartx.phonelightning.service.MsgReceiver
import com.idopartx.phonelightning.service.SmsService
import com.idopartx.phonelightning.service.TelListenerService
import com.idopartx.phonelightning.ui.base.BaseActivity
import com.idopartx.phonelightning.utils.SmsObserver
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.dotools.toutiaolibrary.util.SplashClickEyeUtils
import com.idopartx.phonelightning.service.NotificationMonitorService
import com.idopartx.phonelightning.utils.PreferenceSetting
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*


class MainActivity : BaseActivity<ActivityMainBinding>() {

    private var isSplash = false
    private val mDateFormatYear: DateFormat = SimpleDateFormat("yyyyMMdd")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initSplashClickEyeData()
        binding.navView.itemIconTintList = null
        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        binding.navView.setupWithNavController(navController)

//        val msgReceiver = MsgReceiver();
//        val intentFilter = IntentFilter();
//        // 2. 设置接收广播的类型
//        intentFilter.priority = Int.MAX_VALUE
//        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED")
//        // 3. 动态注册：调用Context的registerReceiver（）方法
//        registerReceiver(msgReceiver, intentFilter)
        checkShowThumbUp()
    }

    override fun initLayoutResId(): Int {
        return R.layout.activity_main
    }


    private fun checkShowThumbUp() {
        //显示好评弹窗
        if (!PreferenceSetting.getShowThumbUped(applicationContext)) {
            val oldDate = PreferenceSetting.getLastRunTime(applicationContext) //获取当前时间
            val curDate = Date(System.currentTimeMillis())
            val str = mDateFormatYear.format(curDate)
            if (PreferenceSetting.getShowThumbUp(applicationContext) && (oldDate == 0L || str.toLong() > oldDate)
            ) {
                val dialog = PraiseDialog(this, object : PraiseDialog.PraiseBtnClickListener {
                    override fun onCheckedClick(isChecked: Boolean) {
                        PreferenceSetting.setShowThumbUped(applicationContext, isChecked)
                    }

                    override fun onThumbUpClick() {
                        PreferenceSetting.setShowThumbUped(applicationContext, true)
                    }

                    override fun onComplainClick() {
                        startActivity(Intent(applicationContext, WebFeedBackActivity::class.java))
                        PreferenceSetting.setShowThumbUped(applicationContext, true)
                    }

                    override fun onCloseClick() {
                        val curDate = Date(System.currentTimeMillis())
                        val str = mDateFormatYear.format(curDate)
                        PreferenceSetting.saveLastRunTime(applicationContext, str.toLong())
                    }
                })
                dialog.show()
            } else {
                PreferenceSetting.setShowThumbUp(applicationContext, true)
            }
        }
    }


    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if (intent != null) {
            isSplash = intent.getBooleanExtra("isSplash", false)
        }
        if (isSplash) {
            initSplashClickEyeData()
        }
    }

    //显示点睛开屏
    private fun initSplashClickEyeData() {
        SplashClickEyeUtils.initSplashClickEyeData(
            window.decorView as ViewGroup,
            findViewById(android.R.id.content)
        )
    }

}